package com.example.lxj.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.lxj.base.exception.BussinessFailedException;
import com.example.lxj.system.entity.SysUser;
import com.example.lxj.system.entity.SysUserRole;
import com.example.lxj.system.mapper.SysUserMapper;
import com.example.lxj.system.service.ISysUserRoleService;
import com.example.lxj.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户信息Service业务层处理
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SysUser selectSysUserByUserId(Long userId)
    {
        // 查询用户对应的角色
        SysUserRole sysUserRole = sysUserRoleService.selectSysUserRoleByUserId(userId);
        SysUser sysUser = sysUserMapper.selectSysUserByUserId(userId);
        sysUser.setRoleId(sysUserRole.getRoleId());
        return sysUser;
    }

    /**
     * 查询用户信息列表
     * 
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser)
    {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */

    @Transactional
    @Override
    public int insertSysUser(SysUser sysUser)
    {
        // 优先判断用户名是否存在
        SysUser checkUserExist = sysUserMapper.login(sysUser);
        if(ObjectUtil.isNotNull(checkUserExist)){
            throw new BussinessFailedException("用户名已存在");
        }else{
            sysUser.setCreateTime(DateUtil.date());
            int i = sysUserMapper.insertSysUser(sysUser);
            if(0 < i){
                // 新增用户-角色关系
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(sysUser.getRoleId());
                return sysUserRoleService.insertSysUserRole(sysUserRole);
            }else{
                return -1;
            }
        }
    }

    /**
     * 修改用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysUser(SysUser sysUser)
    {
        sysUser.setUpdateTime(DateUtil.date());
        // 更新用户角色
        SysUserRole sysUserRole = new SysUserRole();
        BeanUtils.copyProperties(sysUser,sysUserRole);
        int i = sysUserRoleService.updateSysUserRole(sysUserRole);
        if(0 < i){
            return sysUserMapper.updateSysUser(sysUser);
        }else{
            return -1;
        }
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserIds(Long[] userIds)
    {
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysUserByUserId(Long userId)
    {
        // 删除用户角色
        int i = sysUserRoleService.deleteSysUserRoleByUserId(userId);
        if(0 < i){
            return sysUserMapper.deleteSysUserByUserId(userId);
        }else{
            return -1;
        }
    }

    /**
     * 重置密码
     * @param sysUser
     * @return
     */
    @Override
    public int resetSysUserPwd(SysUser sysUser) {
        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 注册用户（客户）
     * @param sysUser
     * @return
     */
    @Transactional
    @Override
    public int registerCustomerUser(SysUser sysUser) {
        SysUser checkUserExist = sysUserMapper.login(sysUser);
        if(ObjectUtil.isNotNull(checkUserExist)){
            throw new BussinessFailedException("用户名已存在");
        }else{
            sysUser.setCreateTime(DateUtil.date());
            sysUser.setUserType("11");
            sysUser.setStatus("1");
            int i = sysUserMapper.insertSysUser(sysUser);
            if(0 < i){
                // 新增用户-角色关系
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(1L);
                return sysUserRoleService.insertSysUserRole(sysUserRole);
            }else{
                return -1;
            }
        }
    }
}
